var app = getApp();
const pageSize = 15;
const bindStatusOption = [{
  value: "",
  name: "全部"
}, {
  value: 1,
  name: "未绑定"
}, {
  value: 2,
  name: "已绑定"
}, {
  value: 3,
  name: "已注销"
}]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFilter: false,
    cardList: [],
    nomore: false,
    loadmore: false,
    page: 1, //当前页数
    empty: false,
    franchisers: [], //加盟商
    conditions: {
      bindStatusIndex: "",
      cardSn: "",
      startTime: "",
      endTime: "",
      franchiserIndex: "",
    },
    bindStatusOption,
    total: 0,
    total_unbind:0,
    total_bind:0,
    total_cancel:0,
    is_franchiser: false,
    drawer: false,
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      is_franchiser: options.is_franchiser ? true : false,
    })
    if (this.data.is_franchiser) {
      this.getPartnerList()
    }
    this.getcardList();
  },

  copyCardNumber(e) {
    const sn = e.currentTarget.dataset.sn;
    wx.setClipboardData({
      data: sn,
    })
  },

  reloadData() {
    this.setData({
      page: 1,
      loadmore: false,
      nomore: false,
      cardList: [],
      total: 0,
    })
    wx.showLoading()
    this.getcardList();
  },


  showDrawer() {
    this.condition = {
      ...this.data.conditions
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      conditions: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.conditions;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);
    this.reloadData();
  },

  onPullDownRefresh: function () {
    this.reloadData();
  },



  handleCondition(e) {
    const name = e.currentTarget.dataset.name;
    this.setData({
      [`conditions.${name}`]: e.detail.value
    })
  },


  //获取加盟商列表
  getPartnerList() {
    app.ajax({
      url: 'Partner/Franchiser/franchiserList',
      data: {
        row: 1000
      },
      success: res => {
        if (res.data.code > 0) {
          this.setData({
            franchisers: res.data.data
          })
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        app.showToast("网络错误")
      }
    })
  },

  // 消费记录
  costre: function (e) {
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '../consumeRecord/consumeRecord?id=' + e.currentTarget.dataset.id+'&sn=' + e.currentTarget.dataset.sn,
      success: function (res) {},
      fail: function (res) {},
      complete: function (res) {},
    })
  },

  //获取卡库列表
  getcardList() {
    var that = this;
    const param = {
      page: that.data.page,
      row: pageSize,
      start_time: that.data.conditions.startTime,
      end_time: that.data.conditions.endTime,
      status: that.data.conditions.bindStatusIndex !== "" ? that.data.bindStatusOption[that.data.conditions.bindStatusIndex].value : '',
      card_sn: that.data.conditions.cardSn,
    }
    if (this.data.is_franchiser) {
      param.join_trader = that.data.conditions.franchiserIndex !== "" ? that.data.franchisers[that.data.conditions.franchiserIndex].id : "all";
    }
    app.ajax({
      url: 'Partner/WaterCard/waterCardLibrary',
      data: param,
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 1000) {
          const source = res.data.data;
          let cardList = that.data.cardList.concat(source.data);
          that.setData({
            drawer: false,
            cardList: cardList,
            total: source.page.total_number,
            nomore: source.page.total_number <= cardList.length,
            loadmore: source.page.total_number > cardList.length,
            empty: source.page.page_count == 0 ? true : false,
            total_unbind:source.page.total_unbind,
            total_bind:source.page.total_bind,
            total_cancel:source.page.total_cancel,
          })
        }
      },
      error: e => {
        wx.hideLoading()
      }
    })
  },

  onReachBottom: function () {
    if (this.data.loadmore) {
      this.setData({
        page: this.data.page + 1
      })
      this.getcardList();
    }
  },


})